package net.pinnoocle.playweb.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.pinnoocle.playcommon.response.BaseResult;
import net.pinnoocle.playdao.entity.PayInfo;
import net.pinnoocle.playdao.entity.page.PageModel;
import net.pinnoocle.playdao.entity.web.order.*;
import net.pinnoocle.playservice.service.impl.OrderServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * @author ye
 * @Description
 * @create 2021-11-01 14:48
 */
@RestController
@RequestMapping("/order")
@Api(tags = "订单相关")
public class OrderController {

    @Autowired
    OrderServiceImpl orderService;

    @GetMapping("payBillPage")
    @ApiOperation("我要买单页回显")
    public BaseResult<PayBillPageVo> payBillPage(){
        return new BaseResult<PayBillPageVo>().ok(orderService.billPage());
    }

    @PostMapping("payBill")
    @ApiOperation("买单")
    public BaseResult<String> payBill(@Validated @RequestBody PayBillParams params){
        String result = orderService.payBill(params);
        return new BaseResult<String>().ok(result);
    }

    @GetMapping("orderList")
    @ApiOperation("用户订单列表")
    public BaseResult<PageModel<UserOrderListPageVo>> orderList(UserOrderListSearchVo searchVo){
        return new BaseResult<PageModel<UserOrderListPageVo>>().ok(orderService.orderList(searchVo));
    }

    @GetMapping("orderDetail")
    @ApiOperation("订单详情")
    public BaseResult<UserOrderDetailVo> orderDetail(String orderId){
        return new BaseResult<UserOrderDetailVo>().ok(orderService.orderDetail(orderId));
    }
    @GetMapping("billGoPay")
    @ApiOperation("订单去支付")
    public BaseResult<PayInfo> billGoPay(@RequestParam("orderId") String orderId) throws Exception {
        return new BaseResult<PayInfo>().ok(orderService.billGoPay(orderId));
    }

    @GetMapping("orderQuery")
    @ApiOperation("订单支付状态查询")
    public BaseResult<String> orderQuery(@RequestParam("orderId") String orderId){
        String payStatus = orderService.orderQuery(orderId);
        if(Objects.equals(payStatus, "success")){
            return new BaseResult<String>().success("success");
        }
        return new BaseResult<String>().error("fail");
    }

    @GetMapping("evaluateScript")
    @ApiOperation("剧本评分")
    public BaseResult<String> evaluateScript(@RequestParam("orderId") String orderId,
                                             @RequestParam("starNum") Integer starNum){
        boolean result = orderService.evaluateScript(orderId, starNum);
        if(result){
            return new BaseResult<String>().success("评分成功，感谢您的参与");
        }
        return new BaseResult<String>().error("评分失败，请重试");
    }
}
